<template>
  <el-dialog :visible.sync="value" :before-close="() => $emit('input', false)">
    <template slot="title">
      {{ isCreated ? '新建' : '修改'}}层级
    </template>
    <el-form :model="form" ref="refForm" :rules="rules" label-width="180px">
      <el-form-item
        label="账户类型"
        prop="levelType"
        placeholder="请选择账户类型"
      >
        <el-radio-group
          v-model="form.levelType"
          :disabled="!isCreated"
        >
          <el-radio
            v-for="item in payEnum.USER_TYPE"
            :label="item.value"
            :key="item.value"
            v-show="item.value > -1"
          >
            {{item.title}}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-select v-model="form.sortId" >
          <el-option
            v-for="sort in sortArray"
            :label="sort"
            :value="sort"
            :key="sort"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="层级名称" prop="name">
        <el-col :span="16">
          <el-input v-model="form.name" placeholder="名称"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="用户注册时间" prop="time">
        <el-date-picker
          v-model="form.time"
          type="datetimerange"
          align="right"
          unlink-panels
          :picker-options="pickerDayStep"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item prop="rechargeTimes" label="存款次数大于">
        <el-col :span="16">
          <el-input-number
            v-model="form.rechargeTimes"
            placeholder="请输入存款次数"
            :min="0"
            :controls="false"
            :precision="0"
            :style="{ width: '100%' }"
            class="global-input-number-left-view"
          >
          </el-input-number>
        </el-col>
      </el-form-item>
      <el-form-item prop="rechargeTotal" label="存款总额大于">
        <el-col :span="16">
          <el-input-number
            v-model="form.rechargeTotal"
            placeholder="请输入存款总额"
            :min="0"
            :controls="false"
            :precision="0"
            :style="{ width: '100%' }"
            class="global-input-number-left-view"
          >
          </el-input-number>
        </el-col>
      </el-form-item>
      <el-form-item prop="withdrawTimes" label="提款次数大于">
        <el-col :span="16">
          <el-input-number
            v-model="form.withdrawTimes"
            placeholder="请输入提款次数"
            :min="0"
            :controls="false"
            :precision="0"
            :style="{ width: '100%' }"
            class="global-input-number-left-view"
          >
          </el-input-number>
        </el-col>
      </el-form-item>

      <el-form-item prop="betTotal" label="有效投注大于">
        <el-col :span="16">
          <el-input-number
            v-model="form.betTotal"
            placeholder="请输入有效投注金额"
            :min="0"
            :controls="false"
            :precision="0"
            :style="{ width: '100%' }"
            class="global-input-number-left-view"
          >
          </el-input-number>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-tip>满足以上所有的条件配置，则用户自动进入该层级。</el-tip>
        <el-tip>同时满足多个层级条件，则按照排序从高至低的形式进入层级。</el-tip>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-col :span="16">
          <el-input type="textarea" placeholder="请输入备注" v-model="form.remark"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-tip>注1：升级层级系统实时统计层级数据，降级层级系统每天凌晨1点统计层级数据。</el-tip>
        <el-tip>注2：层级数据统计时间周期为近30天。</el-tip>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button
        @click="cancel"
      >
        取 消
      </el-button>
      <el-button
        type="primary"
        :loading="loading"
        @click="doSave({action: 'saveForm', form: 'refForm'})"
      >
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>

// services
import { editUserLevel } from '@/services/api/userLevel';

// mixins
import { form, pickerOption } from '@/build';

export default {
  name: 'level-edit-dialog',
  props: {
    rowData: {
      type: Object,
      required: true,
      default: () => ({
        itemList: [],
      }),
    },
    value: {
      type: Boolean,
      default: true,
    },
    maxSortId: {
      type: Number,
      default: 2,
    },
  },
  mixins: [form, pickerOption],
  data() {
    return {
      shopNoList: [],
      form: {
        levelType: 0,
        sortId: 2,
        time: [new Date(this.rowData.regFromTime), new Date(this.rowData.regEndTime)],
        name: null,
        rechargeTimes: null,
        rechargeTotal: null,
        withdrawTimes: null,
        betTotal: null,
        ...this.rowData,
      },
      rules: {
        rechargeTimes: [
          { required: true, message: '请输入存款次数' },
        ],
        withdrawTimes: [
          { required: true, message: '请输入提款次数' },
        ],
        rechargeTotal: [
          { required: true, message: '请输入存款总额' },
        ],
        betTotal: [
          { required: true, message: '请输入有效投注金额' },
        ],
        name: [
          { required: true, message: '层级名称不能为空' },
        ],
        time: [
          { required: true, message: '会员注册时间不能为空' },
        ],
      },

    };
  },
  computed: {
    isCreated() {
      return !this.rowData.levelId;
    },
    sortArray() {
      return _.range(2, this.maxSortId + 2);
    },
  },
  methods: {
    cancel() {
      this.$emit('input', false);
    },
  },
  mounted() {
    this.bindSave(editUserLevel, 'saveForm', 'form', {
      beforeSend: (data) => {
        const { time } = data;
        let regFromTime; let
          regEndTime;
        if (!_.isUndefined(time) && _.isArray(time)) {
          regFromTime = _.formatTime(time[0]);
          regEndTime = _.formatTime(time[1]);
        }
        return { ...data, regFromTime, regEndTime };
      },
      afterSuccess: () => {
        this.$emit('input', false);
        this.$parent.getAll();
      },
    });
  },
};
</script>

<style scoped>
</style>
